<?php

/**
 * This is the model class for table "member".
 *
 * The followings are the available columns in table 'member':
 * @property string $id
 * @property string $username
 * @property string $password
 * @property string $type
 * @property string $name
 * @property string $gender
 * @property string $email
 * @property string $phone
 * @property string $birth
 * @property string $education
 * @property string $description
 * @property string $status
 * @property string $created
 * @property string $modified
 *
 * The followings are the available model relations:
 * @property Event[] $events
 * @property Node[] $nodes
 * @property Site[] $sites
 * @property UserGroupRelation[] $userGroupRelations
 */
class Member extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Member the static model class
	 */
	const STATUS_PENDING = 0;
	const STATUS_ACTIVE = 1;
	const STATUS_FEATURED = 2;

	const LOGIN_FROM_LOCAL = 0;
	const LOGIN_FROM_FACEBOOK = 1;
	const LOGIN_FROM_RENREN = 2;

	const TYPE_USER = 0;
	const TYPE_GROUP = 1;
	const TYPE_ADMIN = 6;

	public $avatar;
	public $password2;
	public $oldpassword;

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'member';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('username, type,password,title,gender,email,phone,education,birth,status', 'required'),
				array('username, password, title, email, phone', 'length', 'max'=>45),
				array('username', 'unique','message'=>'<span style="color: #c00">用户名已经被占用</span>'),
				array('type', 'length', 'max'=>5),
				array('password','length','min'=>6),
				array('gender', 'length', 'max'=>1),
				array('avatar', 'file', 'types'=>'jpg,gif,png','allowEmpty'=>true ,'wrongType'=>'只允许 jpg,gif,png'),
				array('education, status', 'length', 'max'=>8),
				array('description', 'length', 'max'=>255),
				array('username,title,email,phone,education,avatar,gender,birth, created, modified', 'safe'),
				// The following rule is used by search().
				// Please remove those attributes that should not be searched.
				array('id, username, password, type, title, gender, email, phone, birth, education, description, status, created, modified', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'events' => array(self::MANY_MANY, 'Event', 'user_event_relation(member_id, event_id)'),
				'nodes' => array(self::HAS_MANY, 'Node', 'member_id'),
				'sites' => array(self::HAS_MANY, 'Site', 'user_id'),
				'userGroupRelations' => array(self::HAS_MANY, 'UserGroupRelation', 'group_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'username' => '用户名',
				'password' => '密码',
				'oldpassword' => '旧密码',
				'password2' => '确认密码',
				'type' => '类型',
				'title' => '姓名',
				'gender' => '性别',
				'email' => '邮箱',
				'phone' => '电话',
				'birth' => '出生日期',
				'education' => '教育背景',
				'description' => '简介',
				'avatar' => "头像",
				'status' => 'Status',
				'created' => 'Created',
				'modified' => 'Modified',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('username',$this->username,true);
		$criteria->compare('password',$this->password,true);
		$criteria->compare('type',$this->type,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('gender',$this->gender,true);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('phone',$this->phone,true);
		$criteria->compare('birth',$this->birth,true);
		$criteria->compare('education',$this->education,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('status',$this->status,true);
		$criteria->compare('created',$this->created,true);
		$criteria->compare('modified',$this->modified,true);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}
}
